﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dicide.ViewModels
{
    public class ConductorsViewModel
    {
        List<ConductorViewModel> _conductorViewModels = new List<ConductorViewModel>();

        public void Add(ConductorViewModel conductorViewModel)
        {
            _conductorViewModels.Add(conductorViewModel);
            conductorViewModel.PropertyChanged += ConductorViewModelPropertyChanged;
        }

        public Boolean Contains(ComponentViewModel conductorViewModel)
        {
            return _conductorViewModels.Contains(conductorViewModel);
        }

        public void DisconnectConductors()
        {
            foreach (var conductorViewModel in _conductorViewModels)
            {
                conductorViewModel.PropertyChanged -= ConductorViewModelPropertyChanged;
            }
            _conductorViewModels.Clear();
        }

        void ConductorViewModelPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "DrivenOn")
            {
                var on = false;

                foreach (var conductorViewModel in _conductorViewModels)
                {
                    on |= conductorViewModel.DrivenOn;
                }

                foreach (var conductorViewModel in _conductorViewModels)
                {
                    conductorViewModel.On = on;
                }
            }
        }
    }
}
